' ***^^^***|||***^^^***
' 
'  This file is regenerated until it's edited. Delete the file to force regeneration.

'  NOTE: This denotes the way the file was created and it's possible the 
'        generation type has since changed.
'        Check the generation script for the curent state of the file.

' %%%###%%%54ffb07248383cac62dfaecbb609dc10%%%###%%%
' %%%##NoWS##%%%7f78e93d3ca8dc5fe2451b014521dace%%%##NoWS##%%%
' ***^^^***|||***^^^***


'************************************************************************************
' Copyright © 2003-2006 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************

Option Explicit On
Option Strict On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports KadGen.Utility2005


Public Class MapObjectDef

   Private mObjectDefFrom As ObjectDefFrom

   Public Sub New(ByVal MapMappingRoot As MapMappingRoot, _
            ByVal TableName As System.String, _
            ByVal ObjectDefFrom As ObjectDefFrom)
      Me.New(MapMappingRoot, Nothing, TableName, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
      Me.mObjectDefFrom = ObjectDefFrom
   End Sub

   Public Sub New(ByVal MapMappingRoot As MapMappingRoot, _
           ByVal TableName As System.String, _
           ByVal ObjectDefFrom As ObjectDefFrom, _
           ByVal ViewName As System.String)
      Me.New(MapMappingRoot, Nothing, TableName, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, ViewName, _
               Nothing, Nothing, Nothing, Nothing, _
               Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
      Me.mObjectDefFrom = ObjectDefFrom
   End Sub

   Public Sub New(ByVal MapMappingRoot As MapMappingRoot, _
                ByVal Name As System.String, _
                ByVal TableName As System.String, _
                ByVal SingularName As System.String, _
                ByVal PluralName As System.String, _
                ByVal DisplayName As System.String, _
                ByVal NonPrefixedOriginalName As System.String, _
                ByVal Prefix As System.String, _
                ByVal Caption As System.String, _
                ByVal PluralCaption As System.String, _
                ByVal DeletePriority As System.Int32, _
                ByVal InsertPriority As System.Int32, _
                ByVal WhereClause As System.String, _
                ByVal Generate As System.String, _
                ByVal NoGenAtAll As System.Boolean, _
                ByVal NoGenObject As System.Boolean, _
                ByVal NoGenStoredProc As System.Boolean, _
                ByVal NoGenUI As System.Boolean, _
                ByVal InheritsFrom As System.String, _
                ByVal IsLookup As System.Boolean, _
                ByVal NoImplicitLookup As System.Boolean, _
                ByVal NoChildren As System.Boolean, _
                ByVal IncludeChildren As System.Boolean, _
                ByVal AlsoIncludeUnderlying As System.Boolean, _
                ByVal NoImplicitForeignKeys As System.Boolean, _
                ByVal ViewName As System.String, _
                ByVal OriginalName As System.String, _
                ByVal ObjectName As System.String, _
                ByVal ObjectDefFrom As ObjectDefFrom, _
                ByVal Schema As System.String, _
                ByVal InferProperties As System.Boolean, _
                ByVal IsChild As Boolean, _
                ByVal IsParent As Boolean, _
                ByVal HasSelfJoin As Boolean, _
                ByVal UnderlyingName As String, _
                ByVal IsMultiParentChild As Boolean, _
                ByVal TableNameColumnName As String, _
                ByVal PrimaryKeyColumnName As String)
      Me.New(MapMappingRoot, Name, TableName, SingularName, PluralName, _
               DisplayName, NonPrefixedOriginalName, Prefix, Caption, _
               PluralCaption, DeletePriority, InsertPriority, WhereClause, _
               Generate, NoGenAtAll, NoGenObject, NoGenStoredProc, _
               NoGenUI, InheritsFrom, IsLookup, NoImplicitLookup, NoChildren, _
               IncludeChildren, AlsoIncludeUnderlying, NoImplicitForeignKeys, ViewName, _
               OriginalName, ObjectName, Schema, InferProperties, IsChild, IsParent, _
               HasSelfJoin, UnderlyingName, IsMultiParentChild, TableNameColumnName, PrimaryKeyColumnName)
      Me.mObjectDefFrom = ObjectDefFrom
   End Sub

   Public Function IsUseForDesc(ByVal propertyName As String) As Boolean
      For Each useForDesc As MapUseForDesc In Me.MapUseForDescs
         If useForDesc.Name = propertyName Then
            Return True
         End If
      Next
      For Each descSet As MapUseForDescSet In Me.MapUseForDescSets
         For Each useForDesc As MapUseForDesc In descSet.MapUseForDescs
            If useForDesc.Name = propertyName Then
               Return True
            End If
         Next
      Next
      Return False
   End Function

   Public Function PrimaryKeys() As MapPropertyCollection
      Dim ret As New MapPropertyCollection()
      For Each prop As MapProperty In Me.MapProperties
         If prop.IsPrimaryKey.GetValueOrDefault AndAlso prop.TableName = Me.TableName Then
            ret.Add(prop)
         End If
      Next
      Return ret
   End Function

   Public Function NonPrimaryKeys() As MapPropertyCollection
      Dim ret As New MapPropertyCollection()
      For Each prop As MapProperty In Me.MapProperties
         If Not prop.IsPrimaryKey.GetValueOrDefault OrElse prop.TableName <> Me.TableName Then
            ret.Add(prop)
         End If
      Next
      Return ret
   End Function

   Public Function GetPropertyByColumnName(ByVal columnName As String) As MapProperty
      For Each prop As MapProperty In Me.MapProperties
         If prop.ColumnName.ToLower() = columnName.ToLower() Then
            Return prop
         End If
      Next
      Return Nothing
   End Function

   Public ReadOnly Property MapMapping() As MapMapping
      Get
         ' This may need to be altered if we include multiple mappings in a mapping root, 
         ' but this is not a currently supported scenario
         Return Me.MapMappingRoot.MapMapping
      End Get
   End Property

   Public Function GetJoin(ByVal tablename As String) As IMapJoin
      For Each join As IMapJoin In Me.MapJoinIns
         If join.TableName = tablename Then
            Return join
         End If
      Next
      For Each join As IMapJoin In Me.MapJoinBases
         If join.TableName = tablename Then
            Return join
         End If
      Next
      Return Nothing
   End Function

   Public Function HasThisChild(ByVal objectDef As MapObjectDef) As Boolean
      For Each child As MapChild In Me.MapChildren
         If child.MapChildObjectDef Is objectDef Then
            Return True
         End If
      Next
   End Function

   Public Function GetChildList() As String
      Dim sb As New Text.StringBuilder
      For Each child As MapChild In Me.MapChildren
         If String.IsNullOrEmpty(child.Name) Then
            sb.AppendLine(child.TableName)
         Else
            sb.AppendLine(child.Name)
         End If
      Next
      Return sb.ToString()
   End Function
End Class

Public Class MapObjectDefCollection
   Public Function GetByTableName(ByVal tableName As String) As MapObjectDefCollection
      ' TODO: Set defaults, perhaps on load.
      Dim ret As New MapObjectDefCollection
      For Each objectDef As MapObjectDef In Me
         If objectDef.TableName = tableName Then
            ret.Add(objectDef)
         End If
      Next
      Return ret
   End Function

   Public Function GetBestByTableName( _
            ByVal tableName As String, _
            ByVal dataStructures As DbsDataStructureCollection) _
            As MapObjectDef
      Dim objectDefs As MapObjectDefCollection = GetByTableName(tableName)
      Return ChooseFromObjectDefs(tableName, objectDefs, dataStructures)
   End Function

   Private Function ChooseFromObjectDefs( _
            ByVal tableName As String, _
            ByVal objectDefs As IList(Of MapObjectDef), _
            ByVal dataStructures As DbsDataStructureCollection) _
            As MapObjectDef
      If objectDefs.Count = 0 Then
         Return Nothing
      ElseIf objectDefs.Count = 1 Then
         Return objectDefs(0)
      Else
         Dim dataStructure As DbsDataStructure = dataStructures(0)
         Dim dbsTable As DbsTable = dataStructure.DbsTables.GetTable(tableName, True, True)
         For Each testObjectDef As MapObjectDef In objectDefs
            If String.Equals(dbsTable.Name, testObjectDef.Name, StringComparison.InvariantCultureIgnoreCase) Then
               Return testObjectDef
            End If
         Next
         Return objectDefs(0)
      End If
   End Function

   Public Function GetBestByTableNameAndChild( _
            ByVal tableName As String, _
            ByVal childTableName As String, _
            ByVal dataStuctures As DbsDataStructureCollection) _
            As MapObjectDef
      Dim objectDefs As MapObjectDefCollection = GetByTableName(tableName)
      If objectDefs.Count = 0 Then
         Return Nothing
      ElseIf objectDefs.Count = 1 Then
         Return objectDefs(0)
      Else
         Dim object2Defs As New List(Of MapObjectDef)
         For Each testObjectDef As MapObjectDef In objectDefs
            If testObjectDef.MapChildren.GetByTableName(childTableName) IsNot Nothing Then
               object2Defs.Add(testObjectDef)
            End If
         Next
         If object2Defs.Count = 0 Then
            Return Nothing
         ElseIf object2Defs.Count = 1 Then
            Return object2Defs(0)
         Else
            Return Me.ChooseFromObjectDefs(tableName, object2Defs, dataStuctures)
         End If
         Return objectDefs(0)
      End If
   End Function


   Public Function GetByObjectName(ByVal objectName As String) As MapObjectDef
      ' TODO: Set defaults, perhaps on load.
      For Each objectDef As MapObjectDef In Me
         If objectDef.Name = objectName Then
            Return objectDef
         End If
      Next
      Return Nothing
   End Function

   Public Function GetByObjectOrTableName(ByVal objectName As String) As MapObjectDef
      ' TODO: Set defaults, perhaps on load.
      For Each objectDef As MapObjectDef In Me
         If objectDef.Name = objectName Then
            Return objectDef
         End If
      Next
      For Each objectdef In Me
         If objectdef.TableName = objectName Then
            Return objectdef
         End If
      Next
      Return Nothing
   End Function

   Default Public Overloads ReadOnly Property Item(ByVal name As String) As MapObjectDef
      Get
         For Each objectDef As MapObjectDef In Me
            If objectDef.Name = name Then
               Return objectDef
            End If
         Next
         Return Nothing
      End Get
   End Property

End Class

